#include<bits/stdc++.h>
using namespace std;
long long v[200005],a[200005];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        long long n,c=0;
        cin>>n;
        for(int i = 1;i<=n;i++){
            cin>>a[i];
            if(a[i-1]!=a[i])v[a[i]]++;
        }
        for(int i = 1;i<=n;i++){
            if(v[a[i]]>1&&a[i+1]!=a[i]){
                c+=a[i];
                v[a[i]]-=2;
            }
            else v[a[i]]=0;
        }
        cout<<c<<"\n";
    }
    return 0;
}
